/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sync.io;

import java.util.logging.Level;
import java.util.logging.Logger;
import env.Parameters;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import sync.model.Assignment;
import sync.model.AssignmentItem;

/**
 *
 * @author yarden
 */
public class Repository {
    
    public final String CHART_READER_CATALOG_FILE = "chartreader.xml";
    
    
    public Repository() {  
    }
    
    
    public String getCatalog() {
        // verify thec catalog file exist
        String filename = getCatalogFilename(); 
        File catalog = new File(filename);
        if (!catalog.exists()) {
            try {
                catalog.createNewFile();
                FileWriter writer = new FileWriter(catalog);
                writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
                writer.close();
            } catch (IOException ex) {
                Logger.getLogger(Repository.class.getName()).log(Level.SEVERE, "Can not create catalog file", ex);
            }
        }
        
        return filename;
    }
    
    
    public void createAssignment(Assignment assignment) {
        String path = getPath()+"/"+assignment.getUser()+"_"+assignment.getId();
        
        File dir = new File(path);
        if (!dir.exists()) {
            // create directory
            dir.mkdir();
            
            // config
            dir = new File(path+"/config");
            dir.mkdir();
            
            // corpus
            dir = new File(path+"/corpus");
            dir.mkdir();
        }
    }
    
    public void saveSchema(Assignment assignment, String schema) {
        String path = getSchemaPath(assignment);
        File file = new File(path);
        try {
            file.createNewFile();
            FileWriter writer = new FileWriter(file);
            writer.write(schema);
            writer.close();
        } catch (IOException ex) {
            Logger.getLogger(Repository.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void saveAssignmentItem(AssignmentItem item, String text) {
        String path = getCorpusPath(item.getAssignment())+File.separator+item.getId()+".txt";
        File file = new File(path);
        try {
            file.createNewFile();
            FileWriter writer = new FileWriter(file);
            writer.write(text);
            writer.close();
        } catch (IOException ex) {
            Logger.getLogger(Repository.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
    public String getSchemaPath(Assignment assignment) {
        return getConfigPath(assignment)+File.separator+"schema.xml";
    }
    
    public String getConfigPath(Assignment assignment) {
        return getAssignmentPath(assignment)+File.separator+"config";
    }
    public String getCorpusPath(Assignment assignment) {
        return getAssignmentPath(assignment)+File.separator+"corpus";
    }
    
    public String getAssignmentPath(Assignment assignment) {
        return getPath()+"/"+assignment.getUser()+"_"+assignment.getId();
    }
     
  
    private String getPath() {
        String path = new File(Parameters.WorkSpace.WorkSpace_AbsolutelyPath).getAbsolutePath();
        if (File.separatorChar != '/') {
            path = path.replace(File.separatorChar, '/');
        }
        if (!path.startsWith("/")) {
            path = "/" + path;
        }
        return path;
    }
    
    private String getTempCatalogURL() {
        return getCatalogFilename()+".tmp";
    }
    
    private String getCatalogFilename() {
        return getPath()+"/"+CHART_READER_CATALOG_FILE;
    }
    
}
